package com.zhixing.service.executor;

import com.zhixing.exception.SwaggerExecutorRunTimeException;
import com.zhixing.model.SwaggerAPI;
import com.zhixing.utils.JsonUtils;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.*;

/**
 * swagger 2 word 抽象执行器
 * @author zhixing
 */
@Slf4j
@Setter
@NoArgsConstructor
public abstract class AbstractSwagger2WordExecutor implements SwaggerExecutor{

    /** swagger json 字符串 **/
    protected String                 swaggerJsonStr;

    /** json str 解析结果集 **/
    protected Map<String, Object>     resultMap;

    /** json str API 结果集 **/
    protected List<SwaggerAPI>        apiResultList;

    /** json str 所有属性解析结果集 **/
    protected Map<String, Object>     allResultMap;

    public AbstractSwagger2WordExecutor(String swaggerJsonStr) {
        this.swaggerJsonStr = swaggerJsonStr;
        this.resultMap = new HashMap<>();
        this.apiResultList = new ArrayList<>();
    }

    /**
     * 处理 Swagger Json 字符串
     * @return
     */
    @Override
    public void executor() {
        try {
            doProcessSwaggerJsonStr();
            log.debug(JsonUtils.writeJsonStr(this.resultMap));
        } catch (Exception e) {
            log.error("parse error", e);
            throw new SwaggerExecutorRunTimeException("executor failed");
        }
    }

    @Override
    public Map<String, Object> getAllResultMap() {
        return Collections.unmodifiableMap(allResultMap);
    }

    @Override
    public List<SwaggerAPI> getApiResultList() {
        return Collections.unmodifiableList(apiResultList);
    }

    protected abstract void doProcessSwaggerJsonStr() throws IOException;


}
